﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.UserControl
{
    public partial class UCPersonChange : RN.ERP.Web.Utilities.BaseUserControl
    {
        public string EmpGuid
        {
            set
            {
                this.hdnEmpGuid.Value = value;
            }
            get
            {
                return this.hdnEmpGuid.Value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PersonServiceClient personService = new PersonServiceClient();
                //人员信息变更项列表
                String personChangeItemJson = personService.GetPersonChangeItems("person", UserInfo.CurrentArea);
                List<Tb_ChangeItem> list = JsonConvert.DeserializeObject<List<Tb_ChangeItem>>(personChangeItemJson);
                this.dlstItem.DataSource = list;
                this.dlstItem.DataBind();
                //选择变更项
                selectItem();
            }
        }

        //选择变更项
        private void selectItem()
        {
            PersonServiceClient personService = new PersonServiceClient();
            String itemType = this.dlstItem.SelectedValue;
            if (itemType != string.Empty)
            {
                //原始值
                String json = personService.GetBeforeChangeValue(itemType, new string[] { EmpGuid }, UserInfo.CurrentArea);
                Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
                this.txtItemOld.Text = dict != null ? dict.Values.First() : "";
                if (itemType == "hEducation")//最高学历
                {
                    this.hdnEducation.Value = dict != null ? dict.Keys.First() : "";//人员学历
                    this.txtItemNew.Text = "";//新值
                    this.txtBeizhu.Text = "";//备注
                    this.txtItemOld.Visible = true;
                    this.txtItemNew.Visible = false;
                    this.dlstHEducationNew.Visible = true;
                    //全部学历
                    String educationJson = personService.GetBindChangeValue("hEducation", UserInfo.CurrentArea, new string[] { });
                    Dictionary<string, string> list = JsonConvert.DeserializeObject<Dictionary<string, string>>(educationJson);
                    this.dlstHEducationNew.DataSource = list;
                    this.dlstHEducationNew.DataBind();
                    if (this.hdnEducation.Value != "")
                    {
                        this.dlstHEducationNew.Items.FindByValue(this.hdnEducation.Value).Selected = true;//默认学历
                    }
                }
                else
                {
                    this.hdnEducation.Value = dict != null ? dict.Keys.First() : "";//人员学历
                    this.txtItemNew.Text = "";//新值
                    this.txtBeizhu.Text = "";//备注
                    this.txtItemOld.Visible = true;
                    this.txtItemNew.Visible = true;
                    this.dlstHEducationNew.Visible = false;
                }
            }
        }
        //选择变更项
        protected void dlstItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            //选择变更项
            selectItem();
        }
    }
}